﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GrainManage.Entities.Trade;
using GrainManage.Proxy;

namespace GrainManage.Controls
{
    /// <summary>
    /// Interaction logic for EditTrade.xaml
    /// </summary>
    public partial class EditTrade : UserControl
    {
        public EditTrade()
        {
            InitializeComponent();
        }

        public void SetTradeInfo(GrainManage.Entities.Table.Trade trade)
        {
            tbTradeId.Text = trade.TradeID.ToString();
            boxContactId.Text = trade.ContactID.ToString();
            cbTradeType.Text = trade.TradeType;
            cbGrainType.Text = trade.Grain;
            boxPrice.Text = trade.Price.ToString();
            boxWeight.Text = trade.Weight.ToString();
            boxActualMoney.Text = trade.ActualMoney.ToString();
        }

        private GrainManage.Entities.Table.Trade GetTradeInfo()
        {
            var trade = new GrainManage.Entities.Table.Trade();
            trade.TradeID = Convertor.ToInt(tbTradeId.Text);
            trade.ContactID = Convertor.ToInt(boxContactId.Text);
            trade.TradeType = cbTradeType.Text;
            trade.Grain = cbGrainType.Text;
            trade.Price = Convertor.ToDecimal(boxPrice.Text);
            trade.Weight = Convertor.ToDecimal(boxWeight.Text);
            trade.ActualMoney = Convertor.ToDecimal(boxActualMoney.Text);
            return trade;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var trade = GetTradeInfo();
            btnSave.IsEnabled = false;
            if (trade.TradeID == 0)
            {
                SafeInvoke.TryBeginInvoke(() =>
                {
                    var input = new InputInsert();
                    input.Trade = trade;
                    input.Authentication = App.Authentication;
                    return Creator.TradeProxy.Insert(input);
                },
               (result) =>
               {
                   btnSave.IsEnabled = true;
               },
               (result) =>
               {
                   if (result.Status.StatusCode != 1)
                   {
                       SafeInvoke.ShowStatus(result.Status.StatusCode, result.Status.Message, Dispatcher);
                   }
                   else
                   {
                       tbTradeId.Text = result.TradeID.ToString();
                   }
               }, Dispatcher);
            }
            else
            {
                SafeInvoke.TryBeginInvoke(() =>
                {
                    var input = new InputUpdate();
                    input.Trade = trade;
                    input.Authentication = App.Authentication;
                    return Creator.TradeProxy.Update(input);
                },
             (result) =>
             {
                 btnSave.IsEnabled = true;
             },
             (result) =>
             {
                 if (result.Status.StatusCode != 1)
                 {
                     SafeInvoke.ShowStatus(result.Status.StatusCode, result.Status.Message, Dispatcher);
                 }
             }, Dispatcher);
            }
        }

    }
}
